$(function() {
    var shopId = getQueryString("shopId");
    // 如果传递shopId就是编辑，否则就是添加
    var isEdit = shopId?true:false;

    // 获取店铺的基础信息加载到也页面来的链接
    var initUrl = '/shopadmin/getshopinitinfo';
    // 店铺注册链接
	var registerShopUrl = '/shopadmin/registeDrshop';
	// 查询店铺信息
    var shopInfoUrl = '/shopadmin/getshopbyid?shopId='+shopId;
    // 修改店铺信息
    var editShopUrl = '/shopadmin/modifyshop';

    if (isEdit) {
        getShopInfo(shopId);
    } else {
        getShopInitInfo();
    }

    // 获取店铺
    function getShopInfo(shopId) {
        $.getJSON(shopInfoUrl,function (data) {
          if(data.success) {
              var shop = data.shop;
              $('#shop-name').val(shop.shopName);
              $('#shop-addr').val(shop.shopAddr);
              $('#shop-phone').val(shop.phone);
              $('#shop-desc').val(shop.shopDesc);
              var shopCategory = "<option data-id='" +
                  shop.shopCategory.shopCategoryId + "' selected>" +
                  shop.shopCategory.shopCategoryName + "</option>";
              var tempAreaHtml = '';
              data.areaList.map(function (item, index) {
                  tempAreaHtml += "<option data-id='" +
                      item.areaId + "'>" +
                      item.areaName + "</option>";
              });
              $('#shop-category').html(shopCategory);
              $('#shop-category').attr('disabled', 'disabled');
              $('#area').html(tempAreaHtml);
              $('#area option[data-id="'+shop.area.areaId+'"]').attr('selected', "selected");
          }
        })
    };
    
	// 获取初始店铺信息
	function getShopInitInfo(){
		$.getJSON(initUrl, function(data) {
			// 获取店铺初始信息成功
			if (data.success){
				var tempHtml = "";
				var tempAreaHtml = "";
                // 店铺类别信息
				data.shopCategoryList.map(function (item, index) {
					tempHtml += '<option data-id="'+item.shopCategoryId+'">' +
						item.shopCategoryName+'</option>';
                });
                // 区域类别信息
                data.areaList.map(function (item, index) {
                    tempAreaHtml += '<option data-id="'+item.areaId+'">' +
                        item.areaName+'</option>';
                });
                $("#shop-category").html(tempHtml);
                $("#area").html(tempAreaHtml);
			}
        });
	};

    $('#submit').click(function() {
        var shop = {};
        // 表单验证待完成

        if(isEdit){
            shop.shopId = shopId;
        }
        shop.shopName = $('#shop-name').val();
        shop.shopAddr = $('#shop-addr').val();
        shop.phone = $('#shop-phone').val();
        shop.shopDesc = $('#shop-desc').val();
        shop.shopCategory = {
            shopCategoryId:$("#shop-category").find("option").not(function () {
                return !this.selected;
            }).data("id")
        };
        shop.area = {
            areaId:$("#area").find("option").not(function () {
                return !this.selected;
            }).data("id")
        };
        // 文件流接收
        var shopImg = $("#shop-img")[0].files[0];
        var formData = new FormData();
        formData.append("shopImg", shopImg);
        formData.append("shopStr", JSON.stringify(shop));
        // 验证码
        var verifyCodeActual = $("#j_captcha").val();
        if (!verifyCodeActual) {
            $.toast("请输入验证码");
            return;
        }
        formData.append("verifyCodeActual", verifyCodeActual);
        $.ajax({
            url: (isEdit?editShopUrl:registerShopUrl),
            type:"POST",
            data:formData,
            contentType:false,
            processData:false,
            cache:false,
            success:function (data) {
                if (data.success){
                    $.toast('提交成功');
                }else{
                    $.toast('提交失败:'+data.errMsg);
                }
                $("#captcha_img").click();
            }
        })
    })
});
